<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/28/028
 * Time: 23:54
 */
namespace app\api\controller;


use think\Request;
use think\Loader;
use think\Db;
class Apply extends Rest{

    public function index(){
        $request = Request::instance();
        $data = $request->param();
        $mobile = $data['telephone'];
        $result = send_mobile_code($mobile);
            $this->data['msg'] = $result;
            $this->data['code'] = 1;
            return $this->data;
    }

    public function userApply(){
        if (1 != self::check_power(1)) {
            return $this->data;
        }

        $request = Request::instance();
        $data = $request->param();
//        $validate = Loader::validate('apply');
            $applyName = $data['Name'];
            $applySex = $data['Sex'];
            $applyBirthday = $data['Birthday'];
            $applyStudentId = $data['StudentId'];
            $applyIdCard = $data['identity'];
            $applyGrade = $data['Grade'];
            $applyDepartment = $data['Department'];
            $applyClass =$data['Class'];//�����˰༶
            $applyTelephone = $data['Telephone'];
            $applyVcCode = $data['VcCode'];
            $applyQQ = $data['QQ'];
            $applyEmail = $data['Email'];
            $applyDmId = $data['DmId'];
            $applyWhy = $data['Why'];
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => "http://apis.haoservice.com/idcard/VerifyIdcard?cardNo=".$applyIdCard."&realName=".$applyName."&key=50fbc9652f1d484f86364251eb613841",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "GET",
            ));
            $response = curl_exec($curl);
            $err = curl_error($curl);
            curl_close($curl);
            $json = json_decode($response);
                $ok = $json->result->isok;
                if($ok){
                    $query_name = Db::name('userinfo')->where(["Name"=>$applyName])->find();
                    $apply_Telephone = Db::name('userinfo')->where(['Telephone' =>$applyTelephone])->find();
                    $apply_StudentId = Db::name('userinfo')->where(['StudentId' =>$applyStudentId])->find();
                    if(!$query_name){
                        if(!$apply_StudentId){
                            if(!$apply_Telephone){
                                $data =[
                                    'Name' => $applyName,
                                    'Sex' => $applySex,
                                    'Birthday' => $applyBirthday,
                                    'IdCard' => $applyIdCard,
                                    'Telephone' => $applyTelephone,
                                    'Email' => $applyEmail,
                                    'StudentID' => $applyStudentId,
                                    'Grade' =>$applyGrade,
                                    'Department' => $applyDepartment,
                                    'Class' => $applyClass,
                                    'DormitoryId' => $applyDmId,
                                    'PrimaryDepartmentA' => 0,
                                    'PrimaryDepartmentS' => 0,
                                    'QQ' => $applyQQ,
                                    'Why' => $applyWhy,
                                    'ApplyTime' => date("Y-m-d H:i:s", time()),
                                    'Type' => 1,
                                    'Position' => 13,
                                    'Head' => "__static__/images/default.jpg"
                                ];
                                $result =  Db::name('userinfo')->insertGetId($data);
                                if($result){
                                    $this->data['msg'] = '报名成功';
                                    $this->data['code'] = 1;
                                }else{
                                    $this->data['code'] = 0;
                                    $this->data['msg']  = '报名失败';
                                }
                            }else{
                                $this->data['msg'] = '当前手机号码已被注册';
                                $this->data['code'] = 0;
                            }
                        }else{
                            $this->data['msg'] = '当前学号已被注册';
                            $this->data['code'] = 0;
                        }
                    }else{
                        $this->data['msg'] = '您已报名成功，无需再次报名';
                        $this->data['code'] = 0;
                    }
                }else{
                    $this->data['msg'] = '姓名与身份证不匹配请填写正确的姓名或身份证号';
                    $this->data['code'] = 0;
                }
        return $this->data;
    }
    public function adminAudit(){

    }
    public function getUserApplyInfo(){

    }


}